package il.ac.braude.sona.gui.list_table;

import il.ac.braude.sona.constants.Resources;
import il.ac.braude.sona.gui.SONAFrame;

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

/**
 * The TableFrame is a SONAFrame that contains all methods to set table model into the frame
 *  
 * @author Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenblum
 */
public class ListTableFrame<T> extends SONAFrame {
	/**
	 * mTable - JTable of this frame
	 */
	protected ListTable<T> listTable;
	/**
	 * Constructs the TableFrame.
	 */
	public ListTableFrame() {
	}
/**
 * 
 * @param title
 * @param table
 * @param listener
 */
	public ListTableFrame(String title,ArrayList<T> table,ListTableClickListener<T> listener) {
		setTitle(title);
		listTable = new ListTable<T>(new ListTableModel<T>(table));
		setTable(listTable);
		addListTableClickListener(listener);
	}
	/**
	 * Constructs the TableFrame with title and table
	 * @param title - the title of this frame
	 * @param table - the table to view in this frame
	 */
	public ListTableFrame(String title,ArrayList<T> table) {
		setTitle(title);
		setTable(listTable=new ListTable<T>(new ListTableModel<T>(table)));
	}
	
	public ListTableFrame(String title) {
		setTitle(title);
	}
	/**
	 * Set the table params to display to user
	 * @param table - the table to set 
	 */
	public void setTable( ListTable<T> table) {
		listTable = table;
		listTable.setRowHeight(30);
		listTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		listTable.setGridColor(Color.LIGHT_GRAY);
		listTable.setShowGrid(true);
		listTable.setShowVerticalLines(false);
		listTable.setShowHorizontalLines(true);
		listTable.getTableHeader().setFont(Resources.Futura20);
		new TableColumnAdjuster(listTable).adjustColumns();
		if (listTable.getModel().getRowCount()<=0) {
			getContentPane().add(new JLabel("No Data to Display",SwingConstants.CENTER));
			return;
		}
		JScrollPane sp = new JScrollPane(listTable);
		getContentPane().add(sp);
		Dimension dim=listTable.getPreferredSize();
		dim.height+=70;
		dim.width+=4;
		sp.setPreferredSize(dim);
	}
	
	public void addListTableClickListener(ListTableClickListener<T> listener) {
		listTable.addListTableClickListener(listener);
	}
}
